# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
steam 浏览更多测评
https://store.steampowered.com/search/?sort_by=_ASC&filter=topsellers
选中一条，点击浏览更多测评
"""



class Steam():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random
            # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
            # 'X-Requested-With': 'XMLHttpRequest'
        }

        self.headers_c = {
            'Accept': 'text/javascript, text/html, application/xml, text/xml, */*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive',
            'Cookie': 'sessionid=470000df681a6ea648155f3d; steamCountry=JP%7C0bf9a8c8fade681e77c6f666ad153f64; timezoneOffset=28800,0; recentlyVisitedAppHubs=578080%2C814380; app_impressions=578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|578080@2_9_100010_|814380@2_9_100010_|814380@2_9_100010_|814380@2_9_100010_|814380@2_9_100010_|814380@2_9_100010_|814380@2_9_100010_',
            'DNT': '1',
            'Host': 'steamcommunity.com',
            'If-Modified-Since': 'Tue, 14 Apr 2020 15:29:00 GMT',
            'Referer': 'https://steamcommunity.com/app/814380/reviews/?browsefilter=toprated&snr=1_5_100010_',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
            'X-Prototype-Version': '1.7',
            'X-Requested-With': 'XMLHttpRequest'
        }

    def get_top(self):
        url = "https://store.steampowered.com/search/results/?query&start=0&count=50&dynamic_data=&sort_by=_ASC&snr=1_7_7_topsellers_7&filter=topsellers&infinite=1"
        html = HttpUtils.do_request("GET", url, self.headers, "")
        data_json = json.loads(html.text)
        html_top = data_json['results_html']
        root = etree.HTML(html_top)
        for r in root.xpath('//a'):
            dict_data = dict()
            dict_data['url'] = r.xpath('./@href')[0]
            dict_data['id'] = dict_data['url'].split('/')[4]
            dict_data['title'] = r.xpath('.//span[@class="title"]/text()')[0]
            dict_data['push_time'] = r.xpath('.//div[@class="col search_released responsive_secondrow"]/text()')[0]
            dict_data['good_bad'] = "".join(r.xpath('.//span[@class="search_review_summary positive"]/@data-tooltip-html'))
            dict_data['price_pre'] = "".join(r.xpath('.//div[@class="col search_price  responsive_secondrow"]/strike/text()')).replace("\r", "").replace("\n", "").strip()
            dict_data['price_now'] = "".join(r.xpath('.//div[@class="col search_price  responsive_secondrow"]/text()')).replace("\r", "").replace("\n", "").strip()
            print(str(dict_data))
            self.get_contents(dict_data['id'])


    # 获取评论参数。依据游戏产品页面
    def get_contents(self, id):
        # url = f"https://steamcommunity.com/app/814380/reviews/?browsefilter=toprated&snr=1_5_100010_"
        url = f"https://steamcommunity.com/app/{id}/reviews/?browsefilter=toprated&snr=1_5_100010_"
        appid = url.split("/")[4]
        html = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(html.text)
        # 全部测评链接
        url_child = root.xpath('//form[@name="MoreContentForm1"]/@action')[0]
        postData = self.get_parms(root)
        postData['appid'] = appid
        self.get_comments(appid, url_child, postData)


    # 获取评论内容
    def get_comments(self, id, url, postData):
        html_comments = HttpUtils.do_request("GET", url, self.headers_c, postData)
        root = etree.HTML(html_comments.text)
        # 提取内容
        for r in root.xpath('//div[@class="apphub_Card modalContentLink interactable"]'):
            dict_data = dict()
            # 推荐
            dict_data['tj'] = "".join(r.xpath('.//div[@class="title"]/text()'))
            # 用户名
            dict_data['username'] = "".join(r.xpath('.//div[@class="apphub_CardContentAuthorName offline ellipsis"]/a/text()'))
            # 拥有产品
            dict_data['product'] = "".join(r.xpath('.//div[@class="apphub_CardContentMoreLink ellipsis"]/text()'))
            # 小时
            dict_data['hours'] = "".join(r.xpath('.//div[@class="hours"]/text()'))
            # 发布时间
            dict_data['create_time'] = r.xpath('.//div[@class="apphub_CardTextContent"]/div[@class="date_posted"]/text()')[0]
            # 内容
            dict_data['txt'] = "".join(r.xpath('.//div[@class="apphub_CardTextContent"]/text()')).replace("\n", "") \
                .replace("\t", "").replace("\r", "")

            print(str(dict_data))
        postData = self.get_parms(root)
        postData['appid'] = id
        self.get_comments(id, url, postData)

    # 获取请求评论参数
    def get_parms(self, root) -> dict:
        dict_data = dict()
        dict_data['userreviewscursor'] = root.xpath('//input[@name="userreviewscursor"]/@value')[0]
        dict_data['userreviewsoffset'] = root.xpath('//input[@name="userreviewsoffset"]/@value')[0]
        dict_data['p'] = root.xpath('//input[@name="p"]/@value')[0]
        dict_data['workshopitemspage'] = root.xpath('//input[@name="workshopitemspage"]/@value')[0]
        dict_data['readytouseitemspage'] = root.xpath('//input[@name="readytouseitemspage"]/@value')[0]
        dict_data['mtxitemspage'] = root.xpath('//input[@name="mtxitemspage"]/@value')[0]
        dict_data['itemspage'] = root.xpath('//input[@name="itemspage"]/@value')[0]
        dict_data['screenshotspage'] = root.xpath('//input[@name="screenshotspage"]/@value')[0]
        dict_data['videospage'] = root.xpath('//input[@name="videospage"]/@value')[0]
        dict_data['artpage'] = root.xpath('//input[@name="artpage"]/@value')[0]
        dict_data['allguidepage'] = root.xpath('//input[@name="allguidepage"]/@value')[0]
        dict_data['webguidepage'] = root.xpath('//input[@name="webguidepage"]/@value')[0]
        dict_data['integratedguidepage'] = root.xpath('//input[@name="integratedguidepage"]/@value')[0]
        dict_data['discussionspage'] = root.xpath('//input[@name="discussionspage"]/@value')[0]
        dict_data['numperpage'] = root.xpath('//input[@name="numperpage"]/@value')[0]
        dict_data['browsefilter'] = root.xpath('//input[@name="browsefilter"]/@value')[0]
        # dict_data['appid'] = root.xpath('//input[@name="appid"]/@value')[0]
        dict_data['appHubSubSection'] = root.xpath('//input[@name="appHubSubSection"]/@value')[0]
        dict_data['l'] = root.xpath('//input[@name="l"]/@value')[0]
        dict_data['filterLanguage'] = root.xpath('//input[@name="filterLanguage"]/@value')[0]
        dict_data['searchText'] = root.xpath('//input[@name="searchText"]/@value')[0]
        dict_data['forceanon'] = root.xpath('//input[@name="forceanon"]/@value')[0]
        return dict_data

    # 获取项目列表
    def get_price_list(self, current):
        url = "https://steamcommunity.com/market/search/render/"
        postData = {
            'query':'',
            'start': current,
            'count': '10',
            'search_descriptions': '0',
            'sort_column': 'popular',
            'sort_dir': 'desc'
        }
        postData1 = {
            'query':'',
            'start': current,
            'count': '10',
            'search_descriptions': '0',
            'sort_column': 'popular',
            'sort_dir': 'desc',
            'appid': '730',
            'category_730_ItemSet[]': 'any',
            'category_730_ProPlayer[]': 'any',
            'category_730_StickerCapsule[]': 'any',
            'category_730_TournamentTeam[]': 'any',
            'category_730_Weapon[]': 'any'
        }
        res = HttpUtils.do_request("GET", url, self.headers, postData1)
        data_json = json.loads(res.text)
        html = data_json['results_html']
        root = etree.HTML(html)
        for r in root.xpath('//a[@class="market_listing_row_link"]'):
            url_child = r.xpath('./@href')[0]
            title = r.xpath('.//span[@class="market_listing_item_name"]/text()')[0]  # 标题
            des = r.xpath('.//span[@class="market_listing_game_name"]/text()')[0]  # 简介
            amount = r.xpath('.//span[@class="market_listing_num_listings_qty"]/text()')[0]  # 数量
            price = r.xpath('.//span[@class="normal_price"]/text()')[0]  # 价格

            time.sleep(1)
            tmp = HttpUtils.do_request("GET", url_child, self.headers, "")
            item_nameid = re.findall(r"Market_LoadOrderSpread(.*?);", tmp.text)[0]  # 获取价格参数id
            item_nameid = item_nameid.replace("(", "").replace(")", "").strip()

            buy_list, sell_list = self.get_price(item_nameid)

            buy_price = buy_list[0][0]  # 出售价格
            buy_amount = buy_list[0][1]  # 出售数量
            print(f"{title},出售价格:{buy_price}")

    # 获取动态价格
    def get_price(self, item_nameid):
        postData = {
            'country': 'HK',
            'language': 'schinese',
            'currency': '1',
            'item_nameid': '175966708',
            'two_factor': '0'
        }
        url = "https://steamcommunity.com/market/itemordershistogram"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        buy_list = data_json['buy_order_graph']  # 出售价格
        sell_list = data_json['sell_order_graph']  # 购买价格

        return buy_list, sell_list


if __name__ == '__main__':
    steam = Steam()
    # steam.get_top()
    # steam.get_contents()

    # 获取装备价格
    for i in range(0, 100, 10):
        steam.get_price_list(i)
